﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XbsWebAPI.Application;
using XbsWebAPI.Core.Tools;
using XbsWebAPI.EntityFrameworkCore.Entities;

namespace XbsWebAPI.Core.Repositories
{
    /// <summary>
    /// 微信小程序基础设置 应用服务层
    /// </summary>
    public class MiniProgramAppService : IMiniProgramAppService
    {
        private readonly IMiniProgramRepository _miniProgramRepository;
        private readonly IMiniProgramImageRepository _miniProgramImageRepository;
        private readonly IMapper _mapper;

        public MiniProgramAppService(IMiniProgramRepository miniProgramRepository, IMiniProgramImageRepository miniProgramImageRepository,IMapper mapper)
        {
            _miniProgramImageRepository = miniProgramImageRepository;
            _miniProgramRepository = miniProgramRepository;
            _mapper = mapper;
        }

        public ResultDto<SetMiniProgramDto> GetMiniProgram()
        {
            var result = new ResultDto<SetMiniProgramDto>();
            try
            {
                var model = new MiniProgram();
                if (_miniProgramRepository.AsQueryable().Any())
                {
                    model = _miniProgramRepository.AsQueryable().Include(a => a.MiniProgramImages).First();
                    var entity = _mapper.Map<SetMiniProgramDto>(model);
                    if(model.MiniProgramImages != null && model.MiniProgramImages.Count > 0)
                    {
                        entity.Files = model.MiniProgramImages.Select(a => a.ImagePath).ToList();
                    }
                    
                    result.Message = "操作成功";
                    result.Result = entity;
                    return result;
                }
                
                result.Message = "操作成功,请做初始化操作";
                result.Result = new SetMiniProgramDto(); 
            }
            catch (Exception e)
            {
                result.Message = e.Message;
                result.Code = -1;
                return result;
            }


            return result;
        }

        public Task<ResultDto<string>> SetMiniProgram(SetMiniProgramDto model,string webPath)
        {
            var result = new ResultDto<string>();

            try
            {
                //1、处理基本信息
                var origin = _miniProgramRepository.AsQueryable().AsNoTracking().First();
                var entity = _mapper.Map<MiniProgram>(model);
                if (origin.Id == Guid.Empty)
                {
                    //新增
                    _miniProgramRepository.CreateAsync(entity);
                }
                else
                {
                    //编辑
                    _miniProgramRepository.UpdateAsync(entity);
                    entity.Id = origin.Id;
                }

                //2、处理上传文件
                var images = _miniProgramImageRepository.Query(a => a.MiniProgramId == entity.Id).ToList();

                //删除老图片
                foreach (var image in images)
                {
                    _miniProgramImageRepository.DeleteAsync(image.Id);
                }

                //新传入的图片路径集合
                //原先ABC，传入CD，先删除ABC，再添加CD
                var filesNames = model.Files;
                if (filesNames != null && filesNames.Count > 0) {
                    //添加新图片
                    foreach (var filePath in filesNames)
                    {
                        var image = new MiniProgramImage
                        {
                            Id = Guid.NewGuid(),
                            ImagePath = filePath,
                            MiniProgramId = entity.Id,
                        };
                        _miniProgramImageRepository.CreateAsync(image);
                    }
                }
                result.Message = "操作成功";
            }
            catch (Exception e)
            {
                result.Code = -1;
                result.Message = e.Message;
                
                return Task.FromResult(result);
            }

            return Task.FromResult(result);
        }
    }
}
